/*
 * @Author: 谢陈洁
 * @Date: 2025-07-14 11:28:26
 * @LastEditors: 谢陈洁
 * @LastEditTime: 2025-07-16 17:02:09
 * @Description: useDeleteConfirm 删除确认
 * @Params: deleteApi 删除接口
 * @Return: handleDelete 删除方法
 * // 使用自定义hooks (启用分页)
  const {
    dataList,
    pagination,
    fetchData,
    handlePaginationChange
  } = useTableData<IArticleItem>(fetchArticleList)

  const { handleDelete } = useDeleteConfirm(deleteArticle)

  // 无分页的使用示例 (如需要)
  // const { dataList, fetchData } = useTableData(fetchArticleList, { hasPagination: false })
 *
 */
import { ElMessageBox, ElMessage } from 'element-plus'

export function useDeleteConfirm(
  deleteApi: (id: string | number) => Promise<any>,
  successCallback?: () => void,
  title?: string
) {
  const handleDelete = async (id: number) => {
    try {
      await ElMessageBox.confirm(title || '确定要删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      await deleteApi(id)
      ElMessage.success('删除成功')
      successCallback?.()
      // const success = await deleteApi(id)
      // if (success) {
      //   ElMessage.success('删除成功')
      //   successCallback?.()
      // }
    } catch (error) {
      // 用户取消删除
    }
  }

  return {
    handleDelete
  }
}
